package jxc.dev;

import java.util.Scanner;

import jxc.base.helper.Encrypt;
import jxc.data.hibernate.dao.OperatorDAO;
import jxc.data.hibernate.pojo.Operator;


/**
 * 用户管理
 * @author WZZ
 *
 */
public class UserAdmin {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		System.out.println("依次输入登录名,密码,用户名,权限，键入!结束程序,#查看所有用户");
		String line=null;
		Operator user=null;
		OperatorDAO od=new OperatorDAO();
		while(true){
			user=new Operator();
			for(int i=0;i<4;i++){
				line=in.nextLine();
				if("!".equals(line))
					return;
				if("#".equals(line)){
					printAll();
					i--;
					continue;
				}
				switch(i){
				case 0:
					user.setUsername(line);
					break;
				case 1:
					user.setPassword(Encrypt.md5(line));
					break;
				case 3:
					user.setPower(line);
					break;
				case 2:
					user.setName(line);
					break;
				}
			}
			od.save(user);
			od.commit();
		}
	}

	public static void printAll(){
		OperatorDAO od=new OperatorDAO();
		for(Operator op:od.listByHQL("from Operator")){
			System.out.println(op.getName());
		}
	}
}
